package iwpl.project.servlet;

import iwpl.project.dao.EmployeesDAO;
import iwpl.project.dao.EquipmentsDAO;
import iwpl.project.dao.RolesDAO;
import iwpl.project.entities.Accounts;
import iwpl.project.entities.Employees;
import iwpl.project.entities.Equipments;
import iwpl.project.entities.Roles;
import iwpl.project.entitymanagerhelper.EntityManagerHelper;

import java.io.IOException;
import java.io.PrintWriter;
import java.util.Iterator;
import java.util.List;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

public class Control_Role extends HttpServlet {

	/**
	 * Constructor of the object.
	 */
	public Control_Role() {
		super();
	}

	public static void ExecuteControl(String Control, Roles role) {
		// TODO add code here
		RolesDAO dao = new RolesDAO();

		if (Control == "Save") {
			EntityManagerHelper.beginTransaction();
			dao.save(role);
			EntityManagerHelper.commit();
		}

		if (Control == "Update") {
			EntityManagerHelper.beginTransaction();
			dao.update(role);
			EntityManagerHelper.commit();
		}

		if (Control == "Delete") {
			EntityManagerHelper.beginTransaction();
			dao.delete(role);
			EntityManagerHelper.commit();
		}
	}

	/**
	 * Destruction of the servlet. <br>
	 */
	public void destroy() {
		super.destroy(); // Just puts "destroy" string in log
		// Put your code here
	}

	/**
	 * The doGet method of the servlet. <br>
	 * 
	 * This method is called when a form has its tag value method equals to get.
	 * 
	 * @param request
	 *            the request send by the client to the server
	 * @param response
	 *            the response send by the server to the client
	 * @throws ServletException
	 *             if an error occurred
	 * @throws IOException
	 *             if an error occurred
	 */
	public void doGet(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {

		/*
		 * response.setContentType("text/html"); PrintWriter out =
		 * response.getWriter(); out.println(
		 * "<!DOCTYPE HTML PUBLIC \"-//W3C//DTD HTML 4.01 Transitional//EN\">");
		 * out.println("<HTML>");
		 * out.println("  <HEAD><TITLE>A Servlet</TITLE></HEAD>");
		 * out.println("  <BODY>"); out.print("    This is ");
		 * out.print(this.getClass()); out.println(", using the GET method");
		 * out.println("  </BODY>"); out.println("</HTML>"); out.flush();
		 * out.close();
		 */
		doProcess(request, response);
	}

	/**
	 * The doPost method of the servlet. <br>
	 * 
	 * This method is called when a form has its tag value method equals to
	 * post.
	 * 
	 * @param request
	 *            the request send by the client to the server
	 * @param response
	 *            the response send by the server to the client
	 * @throws ServletException
	 *             if an error occurred
	 * @throws IOException
	 *             if an error occurred
	 */
	public void doPost(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {

		/*
		 * esponse.setContentType("text/html"); PrintWriter out =
		 * response.getWriter(); out.println(
		 * "<!DOCTYPE HTML PUBLIC \"-//W3C//DTD HTML 4.01 Transitional//EN\">");
		 * out.println("<HTML>");
		 * out.println("  <HEAD><TITLE>A Servlet</TITLE></HEAD>");
		 * out.println("  <BODY>"); out.print("    This is ");
		 * out.print(this.getClass()); out.println(", using the POST method");
		 * out.println("  </BODY>"); out.println("</HTML>"); out.flush();
		 * out.close();
		 */
		doProcess(request, response);
	}

	public void doProcess(HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException {
		Roles role = new Roles();
		RolesDAO dao = new RolesDAO();
		int sid=0;
		int did=0;
		try {
			String action=request.getParameter("action");
			if(request.getParameter("hidrID")!=null)
				sid=Integer.parseInt(request.getParameter("hidrID"));

			if(request.getParameter("rID")!=null)
				did=Integer.parseInt(request.getParameter("rID"));
			
			
			
			String srolename=request.getParameter("txtRoleName");
			Boolean sactive=Boolean.valueOf(request.getParameter("chkActive"));
			String sdescription =request.getParameter("txtDescription");
			/*String sEmployeeID = request.getParameter("cboEmployeeID");*/
			
			
		
			if(action.equals("Delete"))
			{
				if(request.getParameter("rID")!=null)
				{
					role.setId(did);
					
					/*ExecuteControl("Delete",role);*/
					
					EntityManagerHelper.beginTransaction();
					dao.delete(role);
					EntityManagerHelper.commit();
					
					response.sendRedirect("../Manage_Roles.jsp?");
				}
				else
					response.sendRedirect("../Manage_Roles.jsp?");
			}
			if(action.equals("Insert"))
			{
					if(request.getParameter("txtRoleName")!= null)
					{
						// check role ton tai
						String tr = request.getParameter("txtRoleName");
						
					
						List<Roles> tro= dao.findByRoleName(tr);
						Roles tro1 = null;
						for (Iterator<Roles> it = tro.iterator(); it.hasNext();) {
							tro1 = (Roles)it.next();
						}
						if(tro1==null)
						{
							role.setActive(sactive);
							role.setDescription(sdescription);
							role.setRoleName(srolename);
							
							EntityManagerHelper.beginTransaction();
							dao.save(role);
							EntityManagerHelper.commit();
							
							response.sendRedirect("../Insert_Role.jsp?message=inserted");
						}
						else //if(tro1.getRoleName().toLowerCase().equals(tr.toLowerCase())||tro1.equals(null))
						{
							response.sendRedirect("../Insert_Role.jsp?message=Role name was valid");							
						}
						
					}
			}
			if(action.equals("Update"))
				{
					if(request.getParameter("txtRoleName")!= null)
					{
						role.setId(sid);
						role.setActive(sactive);
						role.setRoleName(srolename);
						
						role.setDescription(sdescription);
						EntityManagerHelper.beginTransaction();
						dao.update(role);
						EntityManagerHelper.commit();
						
						response.sendRedirect("../Manage_Roles.jsp?message=updated");
					}
					else
						response.sendRedirect("../Manage_Roles.jsp?message=update failed");				
				}
			
		} catch (Exception e) {
			// TODO: handle exception
			System.out.print(e.getMessage());
		}

		/*
		 * if (sStatusUpdate != null) { role.setId(sRoleID);
		 * role.setRoleName(sRoleName); role.setDescription(sDescription);
		 * 
		 * ExecuteControl("Update", role); // After run command, return main
		 * page response.sendRedirect("../Insert_Roles.jsp"); }
		 */
	}

	/**
	 * Initialization of the servlet. <br>
	 * 
	 * @throws ServletException
	 *             if an error occurs
	 */

	public void init() throws ServletException {
		// Put your code here
	}

}
